import 'package:flutter/material.dart';
import 'package:get/get.dart';

///@author:create by BZY
///Date: 2022/3/28 09:28
///Description: 日期操作相关

/// 获取从当前时间之后[days]的时间段
/// 格式为 yyyy-MM-dd
List<DateTime> getDateRangeDays(int days) {
  var dateList = List<DateTime>.empty(growable: true);
  var dateNow = DateTime.now();

  dateList.add(dateNow);
  for (var i = 0; i < days; i++) {
    dateList.add(DateTime(dateNow.year, dateNow.month, dateNow.day + i + 1));
  }
  return dateList;
}

///比较两个String类型的日期大小
int compareDateStr(String left, String right) {
  return convertString2Date(left).compareTo(convertString2Date(right));
}

///比较两个日期大小,用于排序
int compareDate(DateTime left, DateTime right) {
  return left.compareTo(right);
}

///将string 转化为DateTime
///[date] 格式 yyyy-MM-dd
DateTime convertString2Date(String date) {
  return DateTime.parse(date);
}

///选择日期
Future<DateTime?> selectDate(
    {DateTime? firstDate, DateTime? lastDate, int? dateRang,String? cancelText}) async {
  return await showDatePicker(
      context: Get.context!,
      cancelText: cancelText,
      initialDate: DateTime.now(),
      firstDate: firstDate ?? DateTime.now(),
      lastDate: lastDate ?? DateTime.now().add(Duration(days: dateRang ?? 20)),
      initialDatePickerMode: DatePickerMode.day);
}
